package com.glsc.ngateway.common.base.repo.mysql.cmdb;

import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbCpuDetail;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface CmdbCpuDetailRepository extends JpaRepository<CmdbCpuDetail, Integer>, JpaSpecificationExecutor<CmdbCpuDetail> {
    List<CmdbCpuDetail> findAllByCpuDetailIdIn(List<Long> id);


    @Query(value = "select m from CmdbCpuDetail m where m.isDelete=0 " +
            "and (:#{#paramSearchDto.cpuDetailCode} is null OR m.cpuDetailCode LIKE CONCAT('%', :#{#paramSearchDto.cpuDetailCode},'%') ) " +
            "and (:#{#paramSearchDto.cpuStandardCode} is null OR m.cpuStandardCode= :#{#paramSearchDto.cpuStandardCode}) " +
            "and (:#{#paramSearchDto.sn} is null OR m.sn LIKE CONCAT('%',:#{#paramSearchDto.sn},'%') ) " +
            "and (:#{#paramSearchDto.locationId} is null OR m.locationId= :#{#paramSearchDto.locationId}) " +
            "and (:#{#paramSearchDto.expirateDate} is null OR m.expirateDate= :#{#paramSearchDto.expirateDate}) " +
            "and (:#{#paramSearchDto.deviceCode} is null OR m.deviceCode= :#{#paramSearchDto.deviceCode}) " +
            "and (:#{#paramSearchDto.lifeStatus} is null OR m.lifeStatus= :#{#paramSearchDto.lifeStatus}) " +
            "and (:#{#paramSearchDto.user} is null OR m.user= :#{#paramSearchDto.user}) " +
            "")
    Page<CmdbCpuDetail> searchByParam(Pageable pageable, CmdbCpuDetail paramSearchDto);


    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_cpu_detail set is_delete = :deleted where cpu_detail_code in :codes ", nativeQuery = true)
    void updateIsDeleteByCodes(Integer deleted, List<String> codes);

    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_cpu_detail set is_delete = :deleted where location_id = :deviceDetailId and life_status = 1", nativeQuery = true)
    void updateIsDeleteByDeviceDetailId(Integer deleted, Integer deviceDetailId);

    List<CmdbCpuDetail> findAllBySnAndIsDelete(String sn, Integer noDelete);

    CmdbCpuDetail findByCpuDetailId(Integer cpuDetailId);

    List<CmdbCpuDetail> findAllByCpuDetailCodeIn(List<String> collect);

    List<CmdbCpuDetail> findAllByDeviceCodeAndIsDelete(String deviceCode, Integer noDeleted);

}
